//
//  U9ADLaunch.m
//  U9WatchGame
//
//  Created by u9-baken on 2018/12/3.
//  Copyright © 2018 UUU9. All rights reserved.
//

#import "U9ADLaunch.h"
#import "XHLaunchAd.h"
#import "NYLaunchManager.h"
#import "NYLaunchAdvertisementModel.h"
#import "ADChildViewController.h"
#import "NYNavigationViewController.h"

@interface U9ADLaunch()<XHLaunchAdDelegate>
@end

@implementation U9ADLaunch

+(void)load{
    [self shareManager];
}

+(U9ADLaunch *)shareManager{
    static U9ADLaunch *instance = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken,^{
        instance = [[U9ADLaunch alloc] init];
    });
    return instance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        //在UIApplicationDidFinishLaunching时初始化开屏广告,做到对业务层无干扰,当然你也可以直接在AppDelegate didFinishLaunchingWithOptions方法中初始化
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            //初始化开屏广告
            [self setupXHLaunchAd];
        }];
        
//        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
//            //初始化开屏广告
//            [self setupXHLaunchAd];
//        }];
    }
    return self;
}

-(void)setupXHLaunchAd{
    //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
    [XHLaunchAd setLaunchSourceType:SourceTypeLaunchScreen];
    
    //1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
    //2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
    //3.数据获取成功,配置广告数据后,自动结束等待,显示广告
    //注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
    [XHLaunchAd setWaitDataDuration:3];
    
    //广告数据请求

   NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"launchImage"];
    if (dict) {
        XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
        //广告停留时间
        imageAdconfiguration.duration = 3;
        //广告frame
        imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
        imageAdconfiguration.imageNameOrURLString = dict[@"data"][@"src"];
        //设置GIF动图是否只循环播放一次(仅对动图设置有效)
        imageAdconfiguration.GIFImageCycleOnce = NO;
        //缓存机制(仅对网络图片有效)
        //为告展示效果更好,可设置为XHLaunchAdImageCacheInBackground,先缓存,下次显示
        imageAdconfiguration.imageOption = XHLaunchAdImageDefault;
        //图片填充模式
        imageAdconfiguration.contentMode = UIViewContentModeScaleAspectFill;
        //广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
                imageAdconfiguration.openModel = dict[@"data"][@"href"];
        //广告显示完成动画
        imageAdconfiguration.showFinishAnimate =ShowFinishAnimateLite;
        //广告显示完成动画时间
        imageAdconfiguration.showFinishAnimateTime = 0.8;
        //跳过按钮类型
        imageAdconfiguration.skipButtonType = SkipTypeTimeText;
        //后台返回时,是否显示广告
        imageAdconfiguration.showEnterForeground = NO;
        
        //图片已缓存 - 显示一个 "已预载" 视图 (可选)
        //        if([XHLaunchAd checkImageInCacheWithURL:[NSURL URLWithString:model.content]]){
        //            //设置要添加的自定义视图(可选)
        //            imageAdconfiguration.subViews = [self launchAdSubViews_alreadyView];
        //
        //        }
        //显示开屏广告
        [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
        
        
    }
    
    [NYLaunchManager getLaunchAdvertisementWithCompletionHandle:^(NYLaunchAdvertisementModel *model, NSError *error) {
        if (!error) {
        }
    }];
}

#pragma mark - XHLaunchAd delegate - 其他
/**
 广告点击事件回调
 */
-(void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenModel:(id)openModel clickPoint:(CGPoint)clickPoint{
    /** openModel即配置广告数据设置的点击广告时打开页面参数(configuration.openModel) */
    if(IsStrEmpty(openModel)) return;
    
    ADChildViewController *childVC = [[ADChildViewController alloc] initWithURL:openModel];
    NYNavigationViewController *navigation = [[NYNavigationViewController alloc] initWithRootViewController:childVC];

//    //此处不要直接取keyWindow
    UIViewController* rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    [rootVC presentViewController:navigation animated:YES completion:nil];
}

@end
